package cn.tedu.egnotice1.controller;

import cn.tedu.egnotice1.base.response.JsonResult;
import cn.tedu.egnotice1.mapper.NoticeMapper;
import cn.tedu.egnotice1.pojo.dto.NoticeAddParam;
import cn.tedu.egnotice1.pojo.dto.NoticeListQuery;
import cn.tedu.egnotice1.pojo.entity.Notice;
import cn.tedu.egnotice1.pojo.vo.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/notice")
public class NoticeController {
    @Autowired
    public NoticeMapper noticeMapper;

    @PostMapping("add")
    public JsonResult insertNotice(NoticeAddParam noticeAddParam){
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        noticeMapper.insertNotice(notice);

        return JsonResult.success();
    }

    @GetMapping("list")
    public JsonResult noticeList(NoticeListQuery noticeListQuery){
        String title = noticeListQuery.getTitle();
        Integer type = noticeListQuery.getType();
        List<NoticeListVO> noticeListVOS = noticeMapper.selectNotice(title, type);

        return JsonResult.success(noticeListVOS);
    }
}
